package com.example.day19_7_19_1;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day19_7_19_1.adapter.InfoRclAdapter;
import com.example.day19_7_19_1.base.BaseApp;
import com.example.day19_7_19_1.base.BaseFragment;
import com.example.day19_7_19_1.base.BasePresenter;
import com.example.day19_7_19_1.bean.ItemBean;
import com.example.day19_7_19_1.bean.TabBean;
import com.example.day19_7_19_1.presenter.IPresenter;
import com.example.day19_7_19_1.view.IView;
import com.scwang.smartrefresh.header.FlyRefreshHeader;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;

public class TabItemFragment extends BaseFragment implements IView {
    public int mId;
    private RecyclerView mRcl;
    private InfoRclAdapter infoRclAdapter;
    private SmartRefreshLayout smart;

    public TabItemFragment(int id) {
        // Required empty public constructor
        mId = id;
    }
    @Subscribe
    public void getUrl(InfoRclAdapter.WebEvent webEvent){
        Intent intent = new Intent(getActivity(), WebActivity.class);
        intent.putExtra("url",webEvent.webUrl);
        getActivity().startActivity(intent);
    }

    @Override
    protected BasePresenter initPresenter() {
        return new IPresenter();
    }

    @Override
    protected void initData() {
        iPresenter.getData(mId);
        smart.finishLoadMore();
        smart.finishRefresh();
    }

    @Override
    protected void initView(View view) {
        smart = view.findViewById(R.id.smart);
        mRcl = view.findViewById(R.id.rcl);
        mRcl.setLayoutManager(new LinearLayoutManager(BaseApp.sContext));
        mRcl.addItemDecoration(new DividerItemDecoration(BaseApp.sContext,DividerItemDecoration.VERTICAL));
        infoRclAdapter = new InfoRclAdapter(BaseApp.sContext);
        mRcl.setAdapter(infoRclAdapter);
        smart.setRefreshHeader(new WaterDropHeader(BaseApp.sContext));
        smart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                infoRclAdapter.refresh();
                initData();
            }
        });
        smart.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_tab_item;
    }

    @Override
    public void setData(ItemBean itemBean) {
        infoRclAdapter.setList(itemBean.getData().getDatas());
    }

    @Override
    public void setTab(TabBean tabBean) {

    }
}
